package dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import bean.User;

public class RoleDao {
	public  static void save1() {

		Session session = null;
		Transaction tran = null;

		User user = new User();
		user.setUserid(10);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tran = session.beginTransaction();

			session.save(user);
			
			int i=1/0;
			
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			if(session!=null){
				session.close();
			}
		}

	}

	
	
	public  static void save2() {

		Session session = null;
		Transaction tran = null;

		User user = new User();
		user.setUserid(10);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			session = HibernateUtil.getSession();
			System.out.println("rolesession=="+session.hashCode());
			tran = session.beginTransaction();

			session.save(user);
			
			int i=1/0;
			
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			HibernateUtil.closeSession();
		}

	}
	
	
	public static void save3() {

		Session session = null;
		Transaction tran = null;

		User user = new User();
		user.setUserid(10);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			System.out.println("roledao session=="+session.hashCode());
			tran = session.beginTransaction();

			session.save(user);
			int i=2/0;
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
		}

	}



	public static void save4(Session session) {
		
		Transaction tran = null;

		User user = new User();
		user.setUserid(10);
		user.setUsername("user7");
		user.setPassword("123");

		try {
			
			System.out.println("roledao session=="+session.hashCode());
			tran = session.beginTransaction();

			session.save(user);
			int i=2/0;
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
		}
	}

	
	
	
}
